#include "one_picture_video.h"
#include "ui_one_picture_video.h"
#include<QFileDialog>
#include<QImageReader>
#include<mythr_one_picture.h>

one_picture_video::one_picture_video(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::one_picture_video)
{
    ui->setupUi(this);
    id=1;
    ui->progressBar->setValue(0);
    ui->progressBar->setRange(0,100);
    ui->progressBar->hide();


    connect(&this->timer,&QTimer::timeout,[=](){timer.stop();addyinping();});
}

one_picture_video::~one_picture_video()
{
    delete ui;
}

void one_picture_video::setoutfilename(QString outfile)
{
    this->outfilename=outfile;

}

void one_picture_video::addyinping()
{
    if(this->yinpingfile==nullptr)
    {
        emit signal_create_over();
    }
    else
    {


        QString yinp_outfilename(outfilename);
        yinp_outfilename=yinp_outfilename.insert(yinp_outfilename.length()-4,"精听");
        ui->show->appendPlainText("增加音频中...");
        addyiping *yinping=new addyiping (NULL);
        yinping->Addyinping(outfilename,yinpingfile,yinp_outfilename);

        connect(yinping,&addyiping::signal_addyinping_over,yinping,&QObject::deleteLater);
        connect(yinping,&addyiping::signal_outtext,yinping,[=](QString str){ui->show->appendPlainText(str);});
        connect(yinping,&addyiping::signal_addyinping_over,[=](){emit signal_create_over();ui->show->appendPlainText("增加音频完成...");

        this->DeleteFileOrFolder(outfilename);


        });

    }

    return;
}

void one_picture_video::on_pushButton_clicked()
{
     filename=QFileDialog::getOpenFileName(this,"选择背景图","",tr("Images (*.png *.xpm *.jpg)"));


}

void one_picture_video::on_pushButton_2_clicked()
{
    //横屏
    if(ui->checkBox->isChecked()==true)
    {
        QImageReader reader(filename);
        reader.setScaledSize(QSize(720,1280));
        reader.read(&img);
    }
    else
    {
        QImageReader reader(filename);
        //reader.setScaledSize(QSize(1280,720));
        reader.read(&img);
    }

//文件名
    QString title=ui->title->toPlainText();
    if(title==nullptr)
    {
        outfilename=outfilename.insert(outfilename.length()-4,QString("%1")).arg(id);
        id++;
    }
    else
    {
        outfilename=outfilename.insert(outfilename.length()-4,title);
    }

    if(ui->lineEdit->text().isEmpty()==true)
        ui->show->appendPlainText("未设置时长!!!");

    if(this->filename.isEmpty()==true)
        ui->show->appendPlainText("未设置背景图!!!");

//    if(yinpingfile.isEmpty()==true)
//        ui->show->setText("未设置音频!!!");

    time=ui->lineEdit->text().toDouble();
    mythr_one_picture *tomp4=new mythr_one_picture(NULL);
    tomp4->setother(this->img,this->outfilename,time);

    qDebug()<<"outfilename="<<outfilename;
    qDebug()<<"time="<<time;

    connect(tomp4,&QThread::finished,tomp4,&QObject::deleteLater);
    connect(tomp4,&QThread::finished,this,[=](){timer.start(3000);ui->progressBar->hide();});
    connect(tomp4,&mythr_one_picture::signal_time,this,&one_picture_video::slot_showprocrss);

    ui->show->appendPlainText("制作视频中...");
    tomp4->start();
    return;
}

void one_picture_video::on_pushButton_3_clicked()
{
    this->yinpingfile=QFileDialog::getOpenFileName(this,"选择音频","",tr("音频 (*.mp3 *.wav *.wma)"));
}

void one_picture_video::slot_showprocrss(double time)
{
    ui->progressBar->show();ui->progressBar->setValue(time/this->time*100);
}

bool one_picture_video::DeleteFileOrFolder(const QString &strPath)
{
    if (strPath.isEmpty() || !QDir().exists(strPath))//是否传入了空的路径||路径是否存在
        return false;

    QFileInfo FileInfo(strPath);

    if (FileInfo.isFile())//如果是文件
        QFile::remove(strPath);
    else if (FileInfo.isDir())//如果是文件夹
    {
        QDir qDir(strPath);
        qDir.removeRecursively();
    }
    return true;
}
